package com.ruoyi.car.business.web.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.car.business.domain.BusStatement;
import com.ruoyi.car.business.domain.BusStatementItem;
import com.ruoyi.car.business.query.StatementItemQuery;
import com.ruoyi.car.business.service.IBusStatementItemService;
import com.ruoyi.car.business.service.IBusStatementService;
import com.ruoyi.common.core.page.TablePageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 结算单明细Controller
 * 
 * @author ruoyi
 * @date 2022-01-29
 */
@Controller
@RequestMapping("business/statementItem")
public class BusStatementItemController extends BaseController
{
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IBusStatementItemService statementItemService;
    @Autowired
    private IBusStatementService statementService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model) {
        // 先根据 statementId 查询结算单对象
        BusStatement statement = statementService.selectBusStatementById(statementId);
        // 根据结算单对象状态,决定跳转到什么页面
        model.addAttribute("statement", statement);
        if (BusStatement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "itemEdit";
        } else {
            return prefix + "itemDetail";
        }
    }

    //列表
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(StatementItemQuery qo) {
        List list = statementItemService.query(qo);
        return new TablePageInfo(list);
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<BusStatementItem> items){
        statementItemService.saveItems(items);
        return AjaxResult.success("保存成功");
    }

    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult pay(Long statementId){
        statementItemService.pay(statementId);
        return AjaxResult.success("支付成功");
    }

    @RequestMapping("/exportOP")
    @ResponseBody
    public void exportOP(Long statementId, HttpServletResponse response) throws IOException {
        // 在方法上设置下载头
        response.setHeader("Content-Disposition","attachment;filename=itemDetailData.xlsx");
        // 把数据源放进 Workbook
        Workbook wb = statementItemService.exportOP(statementId);
        //把内存中的workbook数据写到浏览器中
        wb.write(response.getOutputStream());
    }
}
